{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "254b837c",
   "metadata": {},
   "source": [
    "## 1. API U本位合约交易账户接口总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "365e9eed",
   "metadata": {},
   "source": [
    "|方法|解释|Path|\n",
    "|:---|:---|:---|\n",
    "|set_positionSide_dual|更改持仓模式|/fapi/v1/positionSide/dual|\n",
    "|get_positionSide_dual|查询持仓模式|/fapi/v1/positionSide/dual|\n",
    "|set_multiAssetsMargin|更改联合保证金模式|/fapi/v1/multiAssetsMargin|\n",
    "|get_multiAssetsMargin|查询联合保证金模式|/fapi/v1/multiAssetsMargin|\n",
    "|set_order|下单|/fapi/v1/order|\n",
    "|set_order_test|测试下单接口 (TRADE)|/fapi/v1/order/test|\n",
    "|alter_order|修改订单|/fapi/v1/order|\n",
    "|set_batchOrders|批量下单|/fapi/v1/batchOrders|\n",
    "|alter_batchOrders|批量修改订单|/fapi/v1/batchOrders|\n",
    "|get_orderAmendment|查询订单修改历史|/fapi/v1/orderAmendment|\n",
    "|get_order|查询订单|/fapi/v1/order|\n",
    "|cancel_order|撤销订单|/fapi/v1/order|\n",
    "|cancel_allOpenOrders|撤销全部订单|/fapi/v1/allOpenOrders|\n",
    "|cancel_batchOrders|批量撤销订单|/fapi/v1/batchOrders|\n",
    "|set_countdownCancelAll|倒计时撤销所有订单|/fapi/v1/countdownCancelAll|\n",
    "|get_openOrder|查询当前挂单|/fapi/v1/openOrder|\n",
    "|get_openOrders|查看当前全部挂单|/fapi/v1/openOrders|\n",
    "|get_allOrders|查询所有订单(包括历史订单)|/fapi/v1/allOrders|\n",
    "|get_balance|账户余额V2|/fapi/v2/balance|\n",
    "|get_account|账户信息V2|/fapi/v2/account|\n",
    "|set_leverage|调整开仓杠杆|/fapi/v1/leverage|\n",
    "|set_marginType|变换逐全仓模式|/fapi/v1/marginType|\n",
    "|set_positionMargin|调整逐仓保证金|/fapi/v1/positionMargin|\n",
    "|get_positionMargin_history|逐仓保证金变动历史|/fapi/v1/positionMargin/history|\n",
    "|get_positionRisk|用户持仓风险V2|/fapi/v2/positionRisk|\n",
    "|get_userTrades|账户成交历史|/fapi/v1/userTrades|\n",
    "|get_income|获取账户损益资金流水|/fapi/v1/income|\n",
    "|get_leverageBracket|杠杆分层标准|/fapi/v1/leverageBracket|\n",
    "|get_adlQuantile|持仓ADL队列估算|/fapi/v1/adlQuantile|\n",
    "|get_forceOrders|用户强平单历史|/fapi/v1/forceOrders|\n",
    "|get_apiTradingStatus|合约交易量化规则指标|/fapi/v1/apiTradingStatus|\n",
    "|get_commissionRate|用户手续费率|/fapi/v1/commissionRate|\n",
    "|get_income_asyn|获取合约资金流水下载Id|/fapi/v1/income/asyn|\n",
    "|get_income_asyn_id|通过下载Id获取合约资金流水下载链接|/fapi/v1/income/asyn/id|\n",
    "|get_order_asyn|获取合约订单历史下载Id|/fapi/v1/order/asyn|\n",
    "|get_order_asyn_id|通过下载Id获取合约订单下载链接|/fapi/v1/order/asyn/id|\n",
    "|get_trade_asyn|获取合约交易历史下载Id|/fapi/v1/trade/asyn|\n",
    "|get_trade_asyn_id|通过下载Id获取合约交易历史下载链接|/fapi/v1/trade/asyn/id|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47612297",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "54e3ebab",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.api import UM\n",
    "from binance_interface.app.utils import eprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "40b4c045",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "um = UM(\n",
    "    key=key, secret=secret,\n",
    "    proxy_host=proxy_host\n",
    ")\n",
    "accountTrade = um.accountTrade"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd444e07",
   "metadata": {},
   "source": [
    "## 3. 账户相关"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa043b8b",
   "metadata": {},
   "source": [
    "### 3.1 账户余额V2 get_balance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "40372e7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'accountAlias': 'SguXuXmYSgTiSgfW',\n",
      "           'asset': 'BTC',\n",
      "           'balance': '0.00000000',\n",
      "           'crossWalletBalance': '0.00000000',\n",
      "           'crossUnPnl': '0.00000000',\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'SguXuXmYSgTiSgfW',\n",
      "           'asset': 'XRP',\n",
      "           'balance': '0.00000000',\n",
      "           'crossWalletBalance': '0.00000000',\n",
      "           'crossUnPnl': '0.00000000',\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'SguXuXmYSgTiSgfW',\n",
      "           'asset': 'TUSD',\n",
      "           'balance': '0.00000000',\n",
      "           'crossWalletBalance': '0.00000000',\n",
      "           'crossUnPnl': '0.00000000',\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'SguXuXmYSgTiSgfW',\n",
      "           'asset': 'BNB',\n",
      "           'balance': '0.00000000',\n",
      "           'crossWalletBalance': '0.00000000',\n",
      "           'crossUnPnl': '0.00000000',\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'SguXuXmYSgTiSgfW',\n",
      "           'asset': 'ETH',\n",
      "           'balance': '0.00000000',\n",
      "           'crossWalletBalance': '0.00000000',\n",
      "           'crossUnPnl': '0.00000000',\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'SguXuXmYSgTiSgfW',\n",
      "           'asset': 'USDT',\n",
      "           'balance': '98.36687032',\n",
      "           'crossWalletBalance': '98.36687032',\n",
      "           'crossUnPnl': '0.00000000',\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'SguXuXmYSgTiSgfW',\n",
      "           'asset': 'USDP',\n",
      "           'balance': '0.00000000',\n",
      "           'crossWalletBalance': '0.00000000',\n",
      "           'crossUnPnl': '0.00000000',\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'SguXuXmYSgTiSgfW',\n",
      "           'asset': 'USDC',\n",
      "           'balance': '0.00000000',\n",
      "           'crossWalletBalance': '0.00000000',\n",
      "           'crossUnPnl': '0.00000000',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "balance_result = accountTrade.get_balance()\n",
    "eprint(balance_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1b0a730",
   "metadata": {},
   "source": [
    "### 3.2 账户信息V2 get_account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aba3b78d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'feeTier': 0,\n",
      "          'canTrade': True,\n",
      "          'canDeposit': True,\n",
      "          'canWithdraw': True,\n",
      "          'tradeGroupId': -1,\n",
      "          'updateTime': 0,\n",
      "          'multiAssetsMargin': False,\n",
      "          'totalInitialMargin': '0.00000000',\n",
      "          'totalMaintMargin': '0.00000000',\n",
      "          'totalWalletBalance': '98.36687032',\n",
      "          'totalUnrealizedProfit': '0.00000000',\n",
      "          'totalMarginBalance': '98.36687032',\n",
      "          'totalPositionInitialMargin': '0.00000000',\n",
      "          'totalOpenOrderInitialMargin': '0.00000000',\n",
      "          'totalCrossWalletBalance': '98.36687032',\n",
      "          'totalCrossUnPnl': '0.00000000',\n",
      "          'availableBalance': '98.36687032',\n",
      "          'maxWithdrawAmount': '98.36687032',\n",
      "          'assets': [{'asset': 'BTC',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     {'asset': 'XRP',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     {'asset': 'TUSD',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     {'asset': 'BNB',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     {'asset': 'ETH',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     '......'],\n",
      "          'positions': [{'symbol': 'SNTUSDT',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        {'symbol': 'SNTUSDT',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        {'symbol': 'SUSHIUSDT',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        {'symbol': 'SUSHIUSDT',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        {'symbol': 'BTSUSDT',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        '......']},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "account_result = accountTrade.get_account()\n",
    "eprint(account_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "668fed9f",
   "metadata": {},
   "source": [
    "### 3.3 用户手续费率 get_commissionRate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3a9e8f2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT', 'makerCommissionRate': '0.000200', 'takerCommissionRate': '0.000500'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "commissionRate_result = accountTrade.get_commissionRate(symbol='MANAUSDT')\n",
    "eprint(commissionRate_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa2d4ca5",
   "metadata": {},
   "source": [
    "### 3.4 账户成交历史 get_userTrades"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4f4d57b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'MANAUSDT',\n",
      "           'id': 483783770,\n",
      "           'orderId': 10483040021,\n",
      "           'side': 'BUY',\n",
      "           'price': '0.4742',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'MANAUSDT',\n",
      "           'id': 483848645,\n",
      "           'orderId': 10484723637,\n",
      "           'side': 'BUY',\n",
      "           'price': '0.4798',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'MANAUSDT',\n",
      "           'id': 483859726,\n",
      "           'orderId': 10485084149,\n",
      "           'side': 'BUY',\n",
      "           'price': '0.4845',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'MANAUSDT',\n",
      "           'id': 483859777,\n",
      "           'orderId': 10485085711,\n",
      "           'side': 'BUY',\n",
      "           'price': '0.4843',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'MANAUSDT',\n",
      "           'id': 483859823,\n",
      "           'orderId': 10485087594,\n",
      "           'side': 'BUY',\n",
      "           'price': '0.4843',\n",
      "           '...': '......'},\n",
      "          '......'],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "userTrade_result = accountTrade.get_userTrades(symbol='MANAUSDT')\n",
    "eprint(userTrade_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c1b603f",
   "metadata": {},
   "source": [
    "## 4. 持仓与杠杆"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5249451c",
   "metadata": {},
   "source": [
    "### 4.1 更改持仓模式 set_positionSide_dual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6f5a62c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': -4059, 'msg': 'No need to change position side.'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# \"true\": 双向持仓模式；\"false\": 单向持仓模式\n",
    "# 如果已经是该持仓模式，返回：{'code': -4059, 'msg': 'No need to change position side.'}\n",
    "accountTrade.set_positionSide_dual(dualSidePosition=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0689e7f6",
   "metadata": {},
   "source": [
    "### 4.2 查询持仓模式 get_positionSide_dual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "515ffb39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': 200, 'data': {'dualSidePosition': True}, 'msg': ''}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accountTrade.get_positionSide_dual() "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e402a5ca",
   "metadata": {},
   "source": [
    "### 4.3 变换逐全仓模式 set_marginType"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cc1825e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': -4046, 'msg': 'No need to change margin type.'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 保证金模式 ISOLATED(逐仓), CROSSED(全仓)\n",
    "# 如果已经是该模式，返回{'code': -4046, 'msg': 'No need to change margin type.'}\n",
    "accountTrade.set_marginType(symbol='MANAUSDT', marginType='ISOLATED')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e26dc527",
   "metadata": {},
   "source": [
    "### 4.4 调整开仓杠杆 set_leverage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "73ae0fcb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': {'symbol': 'MANAUSDT', 'leverage': 1, 'maxNotionalValue': '10000000'}, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_leverage_result = accountTrade.set_leverage(symbol='MANAUSDT', leverage=1)\n",
    "eprint(set_leverage_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28c9d43e",
   "metadata": {},
   "source": [
    "## 5. 交易与订单"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "890ac732",
   "metadata": {},
   "source": [
    "### 5.1 下单 set_order"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2cf56a4",
   "metadata": {},
   "source": [
    "#### 5.1.1 限单价开仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ded33576",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 10518283927,\n",
      "          'symbol': 'MANAUSDT',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'LfDRA0odbXwy559agURLiU',\n",
      "          'price': '0.4000',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '15',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumQuote': '0.0000',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0.0000',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'priceMatch': 'NONE',\n",
      "          'selfTradePreventionMode': 'NONE',\n",
      "          'goodTillDate': 0,\n",
      "          'updateTime': 1706245923758},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 开多\n",
    "set_order_result = accountTrade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    price='0.4',\n",
    "    quantity='15',\n",
    "    side='BUY',\n",
    "    type='LIMIT',\n",
    "    timeInForce='GTC',\n",
    "    positionSide='LONG',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e57abc8",
   "metadata": {},
   "source": [
    "#### 5.1.2 市单价开仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "623f83c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 10518286864,\n",
      "          'symbol': 'MANAUSDT',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'w88GuTXAZy5LB2KDhAJdD2',\n",
      "          'price': '0.0000',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '15',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumQuote': '0.0000',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'MARKET',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'SELL',\n",
      "          'positionSide': 'SHORT',\n",
      "          'stopPrice': '0.0000',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'MARKET',\n",
      "          'priceMatch': 'NONE',\n",
      "          'selfTradePreventionMode': 'NONE',\n",
      "          'goodTillDate': 0,\n",
      "          'updateTime': 1706245988691},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 开空\n",
    "set_order_result = accountTrade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    quantity='15',\n",
    "    side='SELL',\n",
    "    type='MARKET',\n",
    "    positionSide='SHORT',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d119b53",
   "metadata": {},
   "source": [
    "#### 5.1.3 限单价平仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7c4efd42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 10518287024,\n",
      "          'symbol': 'MANAUSDT',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'PQ5ysCRUEYDaejOrKph9ot',\n",
      "          'price': '1.0000',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '12',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumQuote': '0.0000',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': True,\n",
      "          'closePosition': False,\n",
      "          'side': 'SELL',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0.0000',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'priceMatch': 'NONE',\n",
      "          'selfTradePreventionMode': 'NONE',\n",
      "          'goodTillDate': 0,\n",
      "          'updateTime': 1706245995585},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 平多\n",
    "set_order_result = accountTrade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    price='1',\n",
    "    quantity='12',\n",
    "    side='SELL',\n",
    "    type='LIMIT',\n",
    "    timeInForce='GTC',\n",
    "    positionSide='LONG',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a500d8d",
   "metadata": {},
   "source": [
    "#### 5.1.4 市单价平仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "55ac6aad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 10518287813,\n",
      "          'symbol': 'MANAUSDT',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'FeGIIggMT0F8R6XPVN7oVQ',\n",
      "          'price': '0.0000',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '10',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumQuote': '0.0000',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'MARKET',\n",
      "          'reduceOnly': True,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'SHORT',\n",
      "          'stopPrice': '0.0000',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'MARKET',\n",
      "          'priceMatch': 'NONE',\n",
      "          'selfTradePreventionMode': 'NONE',\n",
      "          'goodTillDate': 0,\n",
      "          'updateTime': 1706246012223},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 平空\n",
    "set_order_result = accountTrade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    quantity='10',\n",
    "    side='BUY',\n",
    "    type='MARKET',\n",
    "    positionSide='SHORT',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68ea8057",
   "metadata": {},
   "source": [
    "### 5.2 当前挂单 get_openOrders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1e20307f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 10518287024,\n",
      "           'symbol': 'MANAUSDT',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'PQ5ysCRUEYDaejOrKph9ot',\n",
      "           'price': '1',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '12',\n",
      "           'executedQty': '0',\n",
      "           'cumQuote': '0.0000',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': True,\n",
      "           'closePosition': False,\n",
      "           'side': 'SELL',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'priceMatch': 'NONE',\n",
      "           '...': '......'},\n",
      "          {'orderId': 10518283927,\n",
      "           'symbol': 'MANAUSDT',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'LfDRA0odbXwy559agURLiU',\n",
      "           'price': '0.4000',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '15',\n",
      "           'executedQty': '0',\n",
      "           'cumQuote': '0.0000',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'priceMatch': 'NONE',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "openOrderList_result = accountTrade.get_openOrders(symbol='MANAUSDT')\n",
    "eprint(openOrderList_result,length=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34531cd8",
   "metadata": {},
   "source": [
    "### 5.3 查询订单 get_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "aa81a055",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 10518287024,\n",
      "          'symbol': 'MANAUSDT',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'PQ5ysCRUEYDaejOrKph9ot',\n",
      "          'price': '1.0000',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '12',\n",
      "          'executedQty': '0',\n",
      "          'cumQuote': '0.0000',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': True,\n",
      "          'closePosition': False,\n",
      "          'side': 'SELL',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0.0000',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'priceMatch': 'NONE',\n",
      "          'selfTradePreventionMode': 'NONE',\n",
      "          'goodTillDate': 0,\n",
      "          'time': 1706245995585,\n",
      "          'updateTime': 1706245995585},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "order_result = accountTrade.get_order(\n",
    "    symbol='MANAUSDT',\n",
    "    orderId='10518287024',\n",
    ")\n",
    "eprint(order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fac6945f",
   "metadata": {},
   "source": [
    "### 5.4 修改订单 alter_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b952ff10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 10518283927,\n",
      "          'symbol': 'MANAUSDT',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'LfDRA0odbXwy559agURLiU',\n",
      "          'price': '0.3900',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '15',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumQuote': '0.0000',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0.0000',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'priceMatch': 'NONE',\n",
      "          'selfTradePreventionMode': 'NONE',\n",
      "          'goodTillDate': 0,\n",
      "          'updateTime': 1706246198094},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "alter_order_result = accountTrade.alter_order(\n",
    "    symbol='MANAUSDT',\n",
    "    orderId = '10518283927',\n",
    "    price='0.39',\n",
    "    side='BUY',\n",
    "    quantity = '15',\n",
    ")\n",
    "eprint(alter_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "584dd1bd",
   "metadata": {},
   "source": [
    "### 5.5 批量下单 set_batchOrders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c10ddb7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 10518295761,\n",
      "           'symbol': 'MANAUSDT',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'MLoDryB4R80vjSw0h4BNZ3',\n",
      "           'price': '0.4000',\n",
      "           '...': '......'},\n",
      "          {'orderId': 10518295760,\n",
      "           'symbol': 'MANAUSDT',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'UG137zRT8db55ks4lbTPZt',\n",
      "           'price': '0.4100',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_batchOrders_result = accountTrade.set_batchOrders(\n",
    "    batchOrders = [\n",
    "        dict(symbol='MANAUSDT', price='0.4', quantity='15', side='BUY', type='LIMIT', timeInForce='GTC', positionSide='LONG',),\n",
    "        dict(symbol='MANAUSDT', price='0.41', quantity='15', side='BUY', type='LIMIT', timeInForce='GTC', positionSide='LONG',),\n",
    "    ]\n",
    ")\n",
    "eprint(set_batchOrders_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ba95fca",
   "metadata": {},
   "source": [
    "### 5.6 撤销订单 cancel_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "eaee829f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 10518283927,\n",
      "          'symbol': 'MANAUSDT',\n",
      "          'status': 'CANCELED',\n",
      "          'clientOrderId': 'LfDRA0odbXwy559agURLiU',\n",
      "          'price': '0.3900',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '15',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumQuote': '0.0000',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0.0000',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'priceMatch': 'NONE',\n",
      "          'selfTradePreventionMode': 'NONE',\n",
      "          'goodTillDate': 0,\n",
      "          'updateTime': 1706246225477},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "cancel_order_result = accountTrade.cancel_order(\n",
    "    symbol='MANAUSDT',\n",
    "    orderId='10518283927'\n",
    ")\n",
    "eprint(cancel_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3525cab",
   "metadata": {},
   "source": [
    "### 5.7 撤销单一交易对的所有挂单 cancel_allOpenOrders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1a68ee47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'msg': 'The operation of cancel all open order is done.'}\n"
     ]
    }
   ],
   "source": [
    "cancel_openOrders_result = accountTrade.cancel_allOpenOrders(symbol='MANAUSDT')\n",
    "eprint(cancel_openOrders_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
